<template>
  <div class="bigbox">
    <van-nav-bar
      title="标题"
      left-text="返回"
      right-text="按钮"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    >
      <template #right>
        <van-icon name="search" size="18" />
      </template>
    </van-nav-bar>
    <form action="/">
      <van-search
        v-model="value"
        placeholder="请输入搜索关键词"
        @search="onSearch"
        shape="round"
      />
    </form>
    <div class="lis" @click="onList">
      <van-tree-select
        height="100%"
        :items="items"
        :main-active-index.sync="active"
      >
        <template #content>
          <van-card
            v-for="(item, index) in bind"
            :key="index"
            :price="item.price"
            :desc="'累计' + item.goods_number + '份'"
            :title="item.title"
            :thumb="item.img_big_logo"
            :goods-id="item.goods_id"

             @click="onDetails(item.goods_id,$event)"
          >
            <template #tags>
              <van-tag plain type="danger">Hot</van-tag>
            </template>
            <template #footer>
              <van-button size="small" class="vbtn" :round="true"
                >加入购物车</van-button
              >
            </template>
          </van-card>
        </template>
      </van-tree-select>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";

export default {
  data() {
    return {
      val:"",
      value: "",
      activeKey: 0,
      active: 0,
      items: [
        { text: "童装玩具" },
        { text: "珠宝首饰" },
        { text: "大家电" },
        { text: "男装" },
        { text: "女装" },
        { text: "食品酒水" },
        { text: "邮币乐器" },
        { text: "钟表眼镜" },
      ],
      bind: [],
    };
  },
  methods: {
    onClickLeft() {
      Toast("返回");
    },
    onClickRight() {
      Toast("按钮");
    },
    onSearch(val) {
      console.log(val);
      this.$http({
        method: "get",
        path: "/goods/list",
        params: {
          search: val,
        },
      }).then((res) => {
        // console.log(res);
        this.bind = res.data.list;
      });
    },
    onDetails(goods_id,$event) {
     window.localStorage.setItem("id", goods_id);
      this.$router.push("/xiangqing")
    },
    onList(e) {
      this.$http(
          {
          method:"get",
          path:"/goods/list",
          params:{
            category:e.target.innerText
          }

        }).then(res=>{
          // console.log(res);
          this.bind=res.data.list
        
        }
      )
      
      
    },
  },
  mounted() {
    this.val=window.sessionStorage.val
    if(this.val){
      this.$http({
      method: "get",
      path: "/goods/list",
      params:{
            category:this.val
      }
      }).then((res) => {
      this.bind = res.data.list;
      });
    }else{
      this.$http({
      method: "get",
      path: "/goods/list",
      }).then((res) => {
      this.bind = res.data.list;
      });
    }
   
  },
};
</script>

<style scoped>
.bigbox {
  display: flex;

  flex-direction: column;
}
.lis {
  flex: 1;
}
.show {
  width: 100%;
  height: 100%;
  background-color: pink;
}
.vbtn {
  background: #ee7046;
  color: white;
  font-size: 12px;
}
</style>